﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.DataModel;
using Common.Helpers;
using Common.Repository;
using SurveyAutomation.Properties;

namespace SurveyAutomation
{
    public partial class SurveyAutomation : Form
    {
        private readonly SqLiteDatabase _repo;
        private HtmlElement submitElement;

        public SurveyAutomation()
        {
            InitializeComponent();
             _repo = new SqLiteDatabase();
        }

        private void txtAdddress_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                wbSurvey.Navigate(txtAdddress.Text.Trim());
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            wbSurvey.Navigate(txtAdddress.Text.Trim());
        }

        private void wbSurvey_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            txtAdddress.Text = wbSurvey.Url.ToString();
            lblSurveyStatus.Text = Resources.done_;
        }

        private void wbSurvey_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            // if it's done or not going, we don't want it visible
            webBrowserProgressBar.Visible = (!(e.CurrentProgress <= 0 || e.CurrentProgress == e.MaximumProgress));
            if (webBrowserProgressBar.Visible)
            {
                // this gets the percentage and multiplies it by integer 100
                // giving an int result between 0 and 100
                webBrowserProgressBar.Value = (int)(((float)e.CurrentProgress / (float)e.MaximumProgress) * 100);
            }
            else
            {
                // this is probably complete redundant, since we don't see it
                webBrowserProgressBar.Value = 0;
            }
        }

        private void wbSurvey_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            lblSurveyStatus.Text = Resources.loading____;
            webBrowserProgressBar.MarqueeAnimationSpeed = 80;
            webBrowserProgressBar.Style = ProgressBarStyle.Marquee;
        }

        private void wbSurvey_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowserProgressBar.MarqueeAnimationSpeed = 0;
            webBrowserProgressBar.Style = ProgressBarStyle.Blocks;

            btnPullText.Enabled = true;
            btnSubmit.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var exludeDictionaryElement = UiHelper.Instance.GetExclusionElement();

            if (wbSurvey.Document != null)
            {
                var elements = wbSurvey.Document.GetElementsByTagName("input");

                var randomText = "Random text";
                var randomTextAutoObj = _repo.GetRandomTextAutomation(1);
                if(randomTextAutoObj != null)
                {
                    randomText = randomTextAutoObj.Literal;
                }

                foreach (HtmlElement elem in elements)
                {
                    var nameStr = elem.GetAttribute("name");
                    if (!string.IsNullOrEmpty(nameStr) && !exludeDictionaryElement.Contains(nameStr.ToUpper()))
                    {
                        var eleType = elem.GetAttribute("type");
                        switch (eleType)
                        {
                            case "text":
                                try
                                {
                                    elem.InnerText = randomText;
                                }
                                catch (NotSupportedException notSupportedException)
                                {
                                    // write log here
                                    continue;
                                }
                                catch (System.Runtime.InteropServices.COMException comException)
                                {
                                    // write log here
                                    continue;
                                }
                                catch (Exception exception)
                                {
                                    // write log here
                                    continue;
                                }
                                break;
                            case "radio":
                                break;
                            case "image":
                                if (nameStr.ToLower().IndexOf("submit") > -1)
                                {
                                    submitElement = elem;
                                }
                                break;
                        }
                        
                    }
                }
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if(submitElement != null)
            {
                submitElement.InvokeMember("click");
            }
            else
            {
                UtilsHelper.ShowMessage("Can not find any submitting element in web site", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
            }
        }
    }
}
